package com.example.service.impl;


import com.example.mapper.StudentMapper;
import com.example.pojo.PageResult;
import com.example.pojo.Student;
import com.example.pojo.StudentQueryParam;
import com.example.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 分页查询
     * @param
     * @return
     */
    @Override
    public PageResult<Student> page(StudentQueryParam studentQueryParam) {
        //1.设置分页参数
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        //2.执行查询
        List<Student> studentList = studentMapper.page(studentQueryParam);
        //3.封装结果
        Page<Student> p = (Page<Student>) studentList;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    /**
     * 批量删除学员信息（数组）
     * @param ids
     */
    @Override
    public void delete(Integer[] ids) {
        studentMapper.delete(ids);
    }

    /**
     * 新增学员信息
     * @param student
     */
    @Override
    public void save(Student student) {
        //1.保存基本信息
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.save(student);
    }

    /**
     * 根据id查询学员信息
     * @param id
     * @return
     */
    @Override
    public Student getById(Integer id) {
        return studentMapper.getById(id);
    }

    /**
     * 修改学员信息
     * @param student
     */
    @Override
    public void update(Student student) {
        //1.更新基本信息
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
        //2.更新班级信息
    }

    /**
     * 修改违纪次数
     *
     * @param id
     * @param score
     * @return
     */
    @Override
    public boolean updateViolation(Integer id, Integer score) {
        // 参数校验
        if (id == null || score == null || score < 0) {
            throw new IllegalArgumentException("无效的参数");
        }

        // 更新数据库
        int result = studentMapper.updateViolationScore(id, score);
        return result > 0;
    }
}
